package ext.tianma.bom.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ext.tianma.bom.util.StringUtil;
import wt.doc.WTDocument;
import wt.part.WTPart;

public class ShowService {

	public static String getShowData(String number, String numType, String name) {
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		WTPart part = null;


		// 判断编号类型
		try {
			if ("docType".equals(numType)) {
				if (!StringUtil.isNullOrEmpty(number)) {
					List<WTDocument> docList = new ArrayList<WTDocument>();
					docList = DrawingHelp.getWTDocumentByNumber(number);
					if (docList.size() == 0) {
						String str = "图纸不存在，请重新输入！";
						JSONObject obj = new JSONObject();
						obj.put("str", str);
						array.put(obj);
						object.put("status", "failed");
						object.put("data", array);
					} else {
						array = DrawingHelp.setData(docList);
						object.put("status", "success");
						object.put("data", array);
					}
				} else {
					List<WTDocument> docByName = DrawingHelp.getDocByName(name);
					System.out.println("====docByName222 :"+docByName);
					if (docByName.size() == 0) {
						String str = "图纸不存在，请重新输入！";
						JSONObject obj = new JSONObject();
						obj.put("str", str);
						array.put(obj);
						object.put("status", "failed");
						object.put("data", array);
					} else {
						array = DrawingHelp.setData(docByName);
						object.put("status", "success");
						object.put("data", array);
					}
				}

			} else if ("partType".equals(numType)) {
				if (!StringUtil.isNullOrEmpty(number)) {
					part = DrawingHelp.getWTPartByNumber(number);
					if (part == null) {
						String str = "物料不存在，请重新输入！";
						JSONObject obj = new JSONObject();
						obj.put("str", str);
						array.put(obj);
						object.put("status", "failed");
						object.put("data", array);
					} else {
						List<WTDocument> refDocument = DrawingHelp.getDocList(part);
						List<WTDocument> selectDocList = new ArrayList<WTDocument>();
						if (!StringUtil.isNullOrEmpty(name)) {
							selectDocList = DrawingHelp.selectDocList(refDocument, name);
						} else {
							selectDocList = refDocument;
						}

						array = DrawingHelp.setDataa(selectDocList, number);
						object.put("status", "success");
						object.put("data", array);
					}
				} /*
					 * else { List<WTDocument> docByName = DrawingHelp.getDocByName(name);
					 * if(docByName.size()==0) { String str = "图纸不存在，请重新输入！"; JSONObject obj = new
					 * JSONObject(); obj.put("str", str); array.put(obj); object.put("status",
					 * "failed"); object.put("data", array); } else{ array =
					 * DrawingHelp.setData(docByName); object.put("status", "success");
					 * object.put("data", array); } }
					 */

			} else {
				List<WTDocument> docByName = DrawingHelp.getDocByName(name);
				if (docByName.size() == 0) {
					String str = "图纸不存在，请重新输入！";
					JSONObject obj = new JSONObject();
					obj.put("str", str);
					array.put(obj);
					object.put("status", "failed");
					object.put("data", array);
				} else {
					array = DrawingHelp.setData(docByName);
					object.put("status", "success");
					object.put("data", array);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object.toString();
	}

}
